#region Using Statements
using System;
using System.IO;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using SolanumSolstice.Units;
using SolanumSolstice.Mapping;
using SolanumSolstice.Buildings;
#endregion

namespace SolanumSolstice
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class SolanumSolstice : Microsoft.Xna.Framework.Game
    {
        //CHANGE THIS FOR DEPLOYMENT TO false!!!
        public static bool DEBUGMODE = false;
        
        enum GameStates
        {
            InGame, InGameReady, XboxScreen, XboxScreenReady, StudioScreen, StudioScreenReady, TitleScreen, TitleScreenReady, ControlsScreenReady, ControlsScreen,
            StoryScreenReady, StoryScreen, GameOver, GameOverReady, WinScienceReady, WinScience, WinMilitaryReady, WinMilitary, WinCivilianReady, WinCivilian
        };

        //Add more if needed
        enum Controller
        {
            X, Y, A, B, BACK
        };

        GraphicsDeviceManager graphics;
        ContentManager content;

        bool isYPressed;
        bool trapMode;
        bool lbIsPressed;
        public static bool ScienceWinEnabled;
        public static bool MilitaryWinEnabled;
        public static bool CivilianWinEnabled;

        TimeSpan xboxTime;
        TimeSpan studiosTime;
        TimeSpan storyTime;

        static Map map;
        MiniMap miniMap;
        GameCursor gameCursor;
        GameSelection gameSelection;
        HUD hud;

        GameStates gameState;

        SpriteBatch spritebatch;
        SpriteFont spriteFont;

        List<Human> selectedItems;
        public static List<Human> activeUnits;
        public static List<Zombie> activeZombies;
        public static List<RoadBlock> activeRoadBlocks;
        public static List<Trap> activeTraps;
        public static List<Unit> deadUnits;
        List<RoadBlock> deadRoadBlocks;
        
        public static List<Building> buildings;
        public static List<Building> houseList;
        public static PoliceStation milbase;
        public static ResearchBuilding scibase;
        public static Beacon beacon;
        public static SupplyBuilding woodbase;
        public static SupplyBuilding foodbase;
        public static Base mainbase;
        public static bool showEnemyHP=false;
        public static bool chainLinkFenceUnlocked=false;
        public static bool electricFenceUnlocked=false;
        public static bool trapUnlocked=false;
        public static bool acidTrapUnlocked=false;
        public static bool detectZombiesUnlocked=false;

        //take people out of the building before upgrading

        Texture2D titleScreen;
        Texture2D studioScreen;
        Texture2D xBoxLogoScreen;
        Texture2D gameOver;
        Texture2D controlsScreen;
        Texture2D civilianWin;
        Texture2D scienceWin;
        Texture2D militaryWin;

        public static AudioEngine audioEngine;
        public static SoundBank soundBank;
        public static WaveBank waveBank;

        public static AudioEngine musicEngine;
        public static SoundBank musicSoundBank;
        public static WaveBank musicWaveBank;

        Cue backgroundMusic;
        Cue endingMusic;
        Cue introMusic;


        public SolanumSolstice()
        {
            graphics = new GraphicsDeviceManager(this);
            content = new ContentManager(Services);
        }

        public static Beacon getBeacon()
        {
            return beacon;
        }


        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            audioEngine = new AudioEngine("Content\\Snd\\sounds.xgs");
            soundBank = new SoundBank(audioEngine, "Content\\Snd\\Sound Bank.xsb");
            waveBank = new WaveBank(audioEngine, "Content\\Snd\\Wave Bank.xwb");

            musicEngine = new AudioEngine("Content\\Snd\\musictrack.xgs");
            musicSoundBank = new SoundBank(musicEngine, "Content\\Snd\\Music Sound Bank.xsb");
            musicWaveBank = new WaveBank(musicEngine, "Content\\Snd\\Music Wave Bank.xwb");

            gameState = GameStates.XboxScreenReady;
            


            //InitializeNewGame();

            spritebatch = new SpriteBatch(graphics.GraphicsDevice);

            base.Initialize();


        }

        void InitializeIntroMusic()
        {
            introMusic = musicSoundBank.GetCue("Intro");
        }

        public void InitializeNewGame()
        {


            //this.graphics.ToggleFullScreen();
            backgroundMusic = musicSoundBank.GetCue("Zombiemusic1");
            endingMusic = musicSoundBank.GetCue("Ending and Credits");
            
            isYPressed = false;
            lbIsPressed = false;
            trapMode = false;
            ScienceWinEnabled = false;
            CivilianWinEnabled = false;
            MilitaryWinEnabled = false;
            activeUnits = new List<Human>();
            activeZombies = new List<Zombie>();
            activeRoadBlocks = new List<RoadBlock>();
            activeTraps = new List<Trap>();
            deadRoadBlocks = new List<RoadBlock>();
            deadUnits = new List<Unit>();
            buildings = new List<Building>();
            houseList = new List<Building>();

            map = new Map(content.Load<Texture2D>("Content\\Txtr\\Map\\townplan"),
                content.Load<Texture2D>("Content\\Txtr\\Map\\Alpha-map"),
                content.Load<Texture2D>("Content\\Txtr\\Map\\blankTile"),
                new Vector2(graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Height));

            miniMap = new MiniMap(map, content.Load<Texture2D>("Content\\Txtr\\Map\\miniMapTile"),
               content.Load<Texture2D>("Content\\Txtr\\Sys\\miniMapViewArea"), 2);

            gameCursor = new GameCursor(content.Load<Texture2D>("Content\\Txtr\\Sys\\cursor"), 7);

            gameSelection = new GameSelection(content.Load<Texture2D>("Content\\Txtr\\Sys\\selectionbox"), 0, 0);
            gameSelection.IsActive = false;

            hud = new HUD(content, new Vector2(graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Height), miniMap, 12);



            map.FocusX = 0*32;
            map.FocusY = 6*32;

            //activeZombies.Add(new ZombieCivilian(content, new Vector2(328, 258)));
            //activeZombies.Add(new ZombieCivilian(content, new Vector2(340, 266)));
            //activeZombies.Add(new ZombieCivilian(content, new Vector2(350, 277)));


            /*

            Beacon 3x3: 10, 10 | 10, 11

            Research Building 4x4: 22, 0 | 23, 3

            Wood Supply 4x4: 4, 16 | 7, 17

            Food Supply 4x4: 16, 4 | 17, 7

            Gun Shop 3x3: 7, 22 | 8, 22

            Zombie Spawn Points 3x3: 
            16, 55 | 17, 55
            56, 17 | 56, 18
            49, 53 | 50, 53
             */

            //make a beacon sound sound before here
            beacon = new Beacon(content, "Beacon", new Vector2(10 * 32, 10 * 32),new Vector2(9 * 32, 11 * 32), 0);
            buildings.Add(beacon);
            map.makeVisible(10,10,3,3);
            //           buildings.Add(new Base(content, "Base", new Vector2(5 * 32, 4 * 32), new Vector2(8 * 32, 8 * 32), 3));

 

            milbase = new PoliceStation(content, "Police Station", new Vector2(7 * 32, 22 * 32), new Vector2(8 * 32, 21 * 32), 0);
            buildings.Add(milbase);
            map.makeVisible(7,22,3,3);

            mainbase=new Base(content, "Base", new Vector2(4 * 32, 4 * 32), new Vector2(6 * 32, 8 * 32), 0);
            buildings.Add(mainbase);
            map.makeVisible(4, 4, 4, 4);

            woodbase=new SupplyBuilding(content, "Wood Supply", new Vector2(4 * 32, 16 * 32), new Vector2(8 * 32, 17 * 32), 0, true, false);
            buildings.Add(woodbase);
            map.makeVisible(4, 16, 4, 4);

            foodbase = new SupplyBuilding(content, "Food Supply", new Vector2(16 * 32, 5 * 32), new Vector2(17 * 32, 8 * 32), 0, false, true);
            buildings.Add(foodbase);
            map.makeVisible(16, 5, 3,3);

            scibase=new ResearchBuilding(content, "Research Building", new Vector2(22 * 32, 0 * 32), new Vector2(23 * 32, 4 * 32), 0);
            buildings.Add(scibase);
            map.makeVisible(22, 0, 4, 4);

            buildings.Add(new Spawn(content, "Mall", new Vector2(16 * 32, 55 * 32), new Vector2(17 * 32, 55 * 32), 0, 8, activeZombies, UnitClass.Civilian));
            buildings.Add(new Spawn(content, "Military Base", new Vector2(56 * 32, 17 * 32), new Vector2(56 * 32, 18 * 32), 0, 10, activeZombies, UnitClass.Military));
            buildings.Add(new Spawn(content, "Hospital", new Vector2(49 * 32, 56 * 32), new Vector2(50 * 32, 56 * 32), 0, 7, activeZombies, UnitClass.Science));


            InitializeBuildings();

            selectedItems = new List<Human>();

            beacon.SpawnCivilian();
            beacon.SpawnCivilian();
            beacon.SpawnCivilian();
            beacon.SpawnCivilian();

            beacon.SpawnCop();
            beacon.SpawnCop();
            beacon.SpawnCop();

            beacon.SpawnResearcher();
            beacon.SpawnResearcher();
            beacon.SpawnResearcher();

            beacon.First = false;
            

            if (DEBUGMODE) debugcode();

        }


        public void debugcode()
        {
            trapUnlocked = true;
            acidTrapUnlocked = true;
            chainLinkFenceUnlocked = true;
            electricFenceUnlocked = true;
            showEnemyHP = false;

        }


        /// <summary>
        /// Load your graphics content.  If loadAllContent is true, you should
        /// load content from both ResourceManagementMode pools.  Otherwise, just
        /// load ResourceManagementMode.Manual content.
        /// </summary>
        /// <param name="loadAllContent">Which type of content to load.</param>
        protected override void LoadGraphicsContent(bool loadAllContent)
        {
            if (loadAllContent)
            {
                titleScreen = content.Load<Texture2D>("Content\\Txtr\\Sys\\TitleScreen");
                studioScreen = content.Load<Texture2D>("Content\\Txtr\\Sys\\somastudios");
                xBoxLogoScreen = content.Load<Texture2D>("Content\\Txtr\\Sys\\xbox360logo");
                gameOver = content.Load<Texture2D>("Content\\Txtr\\Sys\\gameover");
                controlsScreen = content.Load<Texture2D>("Content\\Txtr\\Sys\\ControlsScreen");
                civilianWin = content.Load<Texture2D>("Content\\Txtr\\Sys\\civilianWin");
                scienceWin = content.Load<Texture2D>("Content\\Txtr\\Sys\\scienceWin");
                militaryWin = content.Load<Texture2D>("Content\\Txtr\\Sys\\militaryWin");
                spriteFont = content.Load<SpriteFont>("Content\\Fnt\\ArialMedium");
            }

            // TODO: Load any ResourceManagementMode.Manual content
        }


        /// <summary>
        /// Unload your graphics content.  If unloadAllContent is true, you should
        /// unload content from both ResourceManagementMode pools.  Otherwise, just
        /// unload ResourceManagementMode.Manual content.  Manual content will get
        /// Disposed by the GraphicsDevice during a Reset.
        /// </summary>
        /// <param name="unloadAllContent">Which type of content to unload.</param>
        protected override void UnloadGraphicsContent(bool unloadAllContent)
        {
            if (unloadAllContent)
            {
                // TODO: Unload any ResourceManagementMode.Automatic content
                content.Unload();
            }

            // TODO: Unload any ResourceManagementMode.Manual content
        }


        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {


            GamePadState gamePad = GamePad.GetState(PlayerIndex.One);

            switch (gameState)
            {
                case GameStates.XboxScreenReady:
                    soundBank.PlayCue("Xbox_360_Elite");
                    xboxTime = gameTime.TotalGameTime;
                    gameState = GameStates.XboxScreen;
                    break;

                case GameStates.XboxScreen:

                    

                    if (gameTime.TotalGameTime.Seconds > xboxTime.TotalSeconds + 5)
                    {
                        gameState = GameStates.StudioScreenReady;
                    }

                    if (gamePad.Buttons.A == ButtonState.Pressed)
                    {
                        gameState = GameStates.StudioScreenReady;
                    }

                    break;

                case GameStates.StudioScreenReady:

                    soundBank.PlayCue("knife_6");
                    studiosTime = gameTime.TotalGameTime;
                    if (gamePad.Buttons.A == ButtonState.Released)
                    {
                        gameState = GameStates.StudioScreen;
                    }

                    break;

                case GameStates.StudioScreen:

                    if (gameTime.TotalGameTime.Seconds > studiosTime.TotalSeconds + 5)
                    {
                        gameState = GameStates.TitleScreenReady;
                    }

                    if (gamePad.Buttons.A == ButtonState.Pressed)
                    {
                        gameState = GameStates.TitleScreenReady;
                    }

                    break;

                case GameStates.TitleScreenReady:
                    if (endingMusic != null && endingMusic.IsPlaying)
                    {
                        endingMusic.Stop(AudioStopOptions.Immediate);
                    }
                    InitializeIntroMusic();

                    introMusic.Play();
                    gameState = GameStates.TitleScreen;
                    break;

                case GameStates.TitleScreen:
                    if (gamePad.Buttons.Start == ButtonState.Pressed)
                    {
                        
                        soundBank.PlayCue("breakingbuilding2");

                        gameState = GameStates.ControlsScreenReady;

                       
                    }
                    break;

                case GameStates.ControlsScreenReady:

                    if(gamePad.Buttons.Start == ButtonState.Released)
                    {
                    gameState = GameStates.ControlsScreen;
                    }
                    break;

                case GameStates.ControlsScreen:
                    if (gamePad.Buttons.Start == ButtonState.Pressed)
                    {

                        gameState = GameStates.StoryScreenReady;
                    }
                    break;

                case GameStates.StoryScreenReady:
                    if (gamePad.Buttons.Start == ButtonState.Released)
                    {
                        storyTime = gameTime.TotalGameTime;
                        gameState = GameStates.StoryScreen;

                    }
                    break;

                case GameStates.StoryScreen:
                    introMusic.Stop(AudioStopOptions.Immediate);
                    
                    Vector2 meas = spriteFont.MeasureString("Loading...");
                    InitializeNewGame();
                    backgroundMusic.Play();

                   
                        soundBank.PlayCue("beacon");
                        gameState = GameStates.InGame;

                    
                    
                    break;

                case GameStates.InGameReady:

                    break;

                case GameStates.InGame:



                    if (gamePad.Buttons.Back == ButtonState.Pressed)
                    {
                        this.Exit();
                    }

                    //Cursor Movement
                    if (gamePad.ThumbSticks.Left.X < 0 && gameCursor.ScreenPositionX > gameCursor.CursorSpeed)
                    {
                        gameCursor.ScreenPositionX -= gameCursor.CursorSpeed;
                    }
                    if (gamePad.ThumbSticks.Left.X > 0 && gameCursor.ScreenPositionX < graphics.GraphicsDevice.Viewport.Width - gameCursor.CursorSpeed)
                    {
                        gameCursor.ScreenPositionX += gameCursor.CursorSpeed;
                    }
                    if (gamePad.ThumbSticks.Left.Y > 0 && gameCursor.ScreenPositionY > gameCursor.CursorSpeed)
                    {
                        gameCursor.ScreenPositionY -= gameCursor.CursorSpeed;
                    }
                    if (gamePad.ThumbSticks.Left.Y < 0 && gameCursor.ScreenPositionY < graphics.GraphicsDevice.Viewport.Height - gameCursor.CursorSpeed)
                    {
                        gameCursor.ScreenPositionY += gameCursor.CursorSpeed;
                    }


                    //Map Movement
                    if (gamePad.ThumbSticks.Right.X < 0 && map.FocusX > map.MapMoveSpeed)
                    {
                        map.FocusX -= map.MapMoveSpeed;
                    }
                    if (gamePad.ThumbSticks.Right.X > 0 && map.FocusX < map.MapSizeX - map.ScreenSizeX - map.MapMoveSpeed)
                    {
                        map.FocusX += map.MapMoveSpeed;
                    }
                    if (gamePad.ThumbSticks.Right.Y > 0 && map.FocusY > map.MapMoveSpeed)
                    {
                        map.FocusY -= map.MapMoveSpeed;
                    }
                    if (gamePad.ThumbSticks.Right.Y < 0 && map.FocusY < map.MapSizeY - map.ScreenSizeY - map.MapMoveSpeed)
                    {
                        map.FocusY += map.MapMoveSpeed;
                    }

                    //Selection Box
                    if (gamePad.Buttons.X == ButtonState.Pressed && gameSelection.IsActive == false)
                    {

                        gameSelection.SetStartPosition(Map.GetMapPosition(new Vector2(gameCursor.ScreenPositionX, gameCursor.ScreenPositionY)));
                        gameSelection.IsActive = true;
                    }


                    else if (gamePad.Buttons.X == ButtonState.Released && gameSelection.IsActive == true)
                    {
                        gameSelection.IsActive = false;

                        bool HUDup = false;

                        if (hud.SelectedBuilding != null)
                        {
                            if (hud.SelectedBuilding.BuildingType == BuildingType.Research)
                            {
                                ResearchBuilding r = (ResearchBuilding)hud.SelectedBuilding;

                                if (r.ResearchTreeIsVisible)
                                {
                                    HUDup = true;
                                }
                            }
                            else if (hud.SelectedBuilding.BuildingType == BuildingType.Beacon)
                            {
                                Beacon b = (Beacon)hud.SelectedBuilding;

                                if (b.SpawnChooserIsVisible)
                                {
                                    HUDup = true;
                                }
                            }
                        }

                        if (HUDup == false)
                        {
                            selectedItems = GetSelection();
                        }
                    }


                    //Movement Selection
                    if (gamePad.Buttons.A == ButtonState.Pressed)
                    {
                        if (gameCursor.ScreenPositionY < map.ScreenSizeY)
                        {
                            Vector2 mapPosition = Map.GetMapPosition(new Vector2(gameCursor.ScreenPositionX, gameCursor.ScreenPositionY));

                            float posModX = mapPosition.X % 32;
                            float posModY = mapPosition.Y % 32;

                            mapPosition = new Vector2(mapPosition.X - posModX, mapPosition.Y - posModY);

                            if (map.Tiles[(int)mapPosition.X / Tile.TILEWIDTH, (int)mapPosition.Y / Tile.TILEHEIGHT].IsWalkable == true)
                            {
                                foreach (Unit unit in selectedItems)
                                {

                                    unit.SetTarget(mapPosition);

                                }
                            }

                        }


                    }

                    if (gamePad.Buttons.B == ButtonState.Pressed)
                    {
                        selectedItems = new List<Human>();
                        hud.SelectedBuilding = null;

                        foreach (RoadBlock roadBlock in activeRoadBlocks)
                        {
                            if (roadBlock.IsConstructed == false)
                            {
                                roadBlock.IsDead = true;
                            }
                        }

                        foreach (Trap trap in activeTraps)
                        {
                            if (trap.IsConstructed == false)
                            {
                                trap.IsDead = true;
                            }
                        }
                    }

                    bool toggled = false;

                    if (gamePad.Buttons.LeftShoulder == ButtonState.Pressed && lbIsPressed == false && trapUnlocked==true)
                    {
                        toggled = true;
                        lbIsPressed = true;

                        
                            if (trapMode == false)
                            {
                                trapMode = true;
                                
                                

                            }
                            else
                            {
                                trapMode = false;
                            }
                        
                        List<RoadBlock> removeRB = new List<RoadBlock>();
                        List<Trap> removeTraps = new List<Trap>();

                        foreach (RoadBlock roadBlock in activeRoadBlocks)
                        {
                            if (roadBlock.IsConstructed == false)
                            {
                                removeRB.Add(roadBlock);
                            }
                        }

                        foreach (Trap trap in activeTraps)
                        {
                            if (trap.IsConstructed == false)
                            {
                                removeTraps.Add(trap);
                            }
                        }

                        RemoveDeadRoadBlocks(removeRB);
                        RemoveDeadTraps(removeTraps);

                    }
                    else if(gamePad.Buttons.LeftShoulder == ButtonState.Released)
                    {
                        lbIsPressed = false;
                    }

                    if (gamePad.Buttons.Y == ButtonState.Pressed && isYPressed == false || toggled == true)
                    {
                        isYPressed = true;


                        
                        if (trapMode == false)
                        {
                            bool setRoadBlock = false;

                            foreach (RoadBlock rb in activeRoadBlocks)
                            {
                                if (rb.IsConstructed == false)
                                {
                                    setRoadBlock = true;

                                    if (rb.IsAcceptable == true && GetBase().Wood >= rb.WoodCost)
                                    {
                                        GetBase().UseWood(rb.WoodCost);

                                        rb.IsConstructed = true;

                                        rb.HitPoints = 1;

                                        hud.SelectRoadBlock(rb);
                                    }
                                }
                            }


                            if (setRoadBlock == false)
                            {
                                bool hasCivilian = false;

                                foreach (Unit unit in selectedItems)
                                {
                                    if (unit.UnitClass == UnitClass.Civilian)
                                    {
                                        hasCivilian = true;
                                    }
                                }




                                if (hasCivilian == true)
                                {
                                    //NEW ROADBLOCK
                                    RoadBlock rb= new RoadBlock(content, Map.GetMapPosition(new Vector2(gameCursor.ScreenPositionX, gameCursor.ScreenPositionY)));
                                    if (chainLinkFenceUnlocked==true) {
                                                                    rb.MaxHitPoints=rb.MaxHitPoints*2;
                                                                    rb.setContent("Content\\Txtr\\Bldg\\barrier_chainlink_horizontal","Content\\Txtr\\Bldg\\barrier_chainlink_vertical");
                                                                 }
                                    if (electricFenceUnlocked==true) {
                                                                    rb.MaxHitPoints=rb.MaxHitPoints*2; 
                                                                    rb.Damage=20;
                                                                    rb.setContent("Content\\Txtr\\Bldg\\barrier_electric_horizontal","Content\\Txtr\\Bldg\\barrier_electric_vertical");
                                                                     }
                                    activeRoadBlocks.Add(rb);
                                }
                            }
                        }
                        else
                        {

                            
                            bool setTrap = false;

                            foreach (Trap rb in activeTraps)
                            {
                                if (rb.IsConstructed == false)
                                {
                                    setTrap = true;

                                    if (rb.IsAcceptable == true && GetBase().Wood >= rb.WoodCost)
                                    {
                                        GetBase().UseWood(rb.WoodCost);

                                        rb.IsConstructed = true;

                                    }
                                }
                            }


                            if (setTrap == false)
                            {
                                bool hasCivilian = false;

                                foreach (Unit unit in selectedItems)
                                {
                                    if (unit.UnitClass == UnitClass.Civilian)
                                    {
                                        hasCivilian = true;
                                    }
                                }




                                if (hasCivilian == true)
                                {
                                    //NEW TRAP
                                    Trap tr = new Trap(content, Map.GetMapPosition(new Vector2(gameCursor.ScreenPositionX, gameCursor.ScreenPositionY)));
                                    if (SolanumSolstice.acidTrapUnlocked) 
                                    { tr.MaxUses = 1000;
                                    tr.Content = "Content\\Txtr\\Bldg\\trap_acid";
                                    }
                                    activeTraps.Add(tr);
                                }
                            }

                        }
                    }
                    else if (gamePad.Buttons.Y == ButtonState.Released)
                    {
                        isYPressed = false;
                    }

                    //Basic Updates

                    Vector2 mapPos = Map.GetMapPosition(new Vector2(gameCursor.ScreenPositionX, gameCursor.ScreenPositionY));

                    gameSelection.EndX = (int)mapPos.X;
                    gameSelection.EndY = (int)mapPos.Y;

                    //List for removal of units that have died

                    List<Unit> removeDeadUnits = new List<Unit>();



                    //Update each Unit and reveal Map
                    foreach (Unit unit in activeUnits)
                    {

                        unit.Update(gameTime, FindEnemyUnits(unit), FindFriendlyUnits(unit));

                        Vector2 shadowPos = new Vector2(unit.Position.X / Tile.TILEWIDTH, unit.Position.Y / Tile.TILEHEIGHT);

                        for (int x = (int)shadowPos.X - unit.SightRadius; x < (int)shadowPos.X + unit.SightRadius; x++)
                        {
                            for (int y = (int)shadowPos.Y - unit.SightRadius; y < (int)shadowPos.Y + unit.SightRadius; y++)
                            {
                                if (x >= 0 && x < map.Tiles.GetLength(0)
                                    && y >= 0 && y < map.Tiles.GetLength(1)
                                    && Vector2.Distance(unit.Position / Tile.TILEHEIGHT, new Vector2(x, y)) < unit.SightRadius)
                                {
                                    map.Tiles[x, y].IsVisible = true;
                                }
                            }
                        }

                        if (unit.IsDead)
                        {
                            removeDeadUnits.Add(unit);
                        }
                    }

                    //Update Zombies
                    foreach (Zombie zombie in activeZombies)
                    {
                        zombie.Update(gameTime, FindEnemyUnits(zombie), FindFriendlyUnits(zombie));

                        if (zombie.IsDead)
                        {
                            removeDeadUnits.Add(zombie);
                        }
                    }

                    //Update Buildings
                    foreach (Building building in buildings)
                    {
                        building.Update(gameTime, gamePad, gameCursor);
                    }

                    List<RoadBlock> removeDeadRoadBlocks = new List<RoadBlock>();

                    //Update Road Blocks
                    foreach (RoadBlock rb in activeRoadBlocks)
                    {
                        rb.Update(gameTime, gamePad, gameCursor);

                        if (rb.IsDead)
                        {
                            removeDeadRoadBlocks.Add(rb);
                        }
                    }

                    List<Trap> removeDeadTraps = new List<Trap>();

                    foreach (Trap trap in activeTraps)
                    {
                        trap.Update(gameTime, gamePad, gameCursor);

                        if (trap.IsDead)
                        {
                            removeDeadTraps = new List<Trap>();
                        }
                    }

                    // Remove dead units and roadBlocks

                    RemoveDeadUnits(removeDeadUnits);
                    RemoveDeadRoadBlocks(removeDeadRoadBlocks);
                    RemoveDeadTraps(removeDeadTraps);

                    hud.Update(gamePad, gameCursor, activeUnits, selectedItems);

                    if (hud.MapFocusChanged == true)
                    {
                        map.FocusX = (int)hud.NewMapFocus.X;
                        map.FocusY = (int)hud.NewMapFocus.Y;
                        hud.MapFocusChanged = false;
                    }

                    //GameOver Checks
                    if (ScienceWinEnabled)
                    {
                        foreach (Building building in buildings)
                        {
                            if (building.BuildingType == BuildingType.Spawn)
                            {
                                Spawn spawn = (Spawn)building;
                                if (spawn.UnitType == UnitClass.Science && spawn.Zombies.Count == 0)
                                {
                                    gameState = GameStates.WinScienceReady;
                                }
                            }
                        }
                    }

                    if (MilitaryWinEnabled)
                    {
                        foreach (Building building in buildings)
                        {
                            if (building.BuildingType == BuildingType.Spawn)
                            {
                                Spawn spawn = (Spawn)building;
                                if (spawn.UnitType == UnitClass.Military && spawn.Zombies.Count == 0)
                                {
                                    gameState = GameStates.WinMilitaryReady;
                                }
                            }
                        }
                    }

                    if(CivilianWinEnabled)
                    {
                        foreach (Building building in buildings)
                        {
                            if (building.BuildingType == BuildingType.Spawn)
                            {
                                Spawn spawn = (Spawn)building;
                                if (spawn.UnitType == UnitClass.Civilian && spawn.Zombies.Count == 0)
                                {
                                    gameState = GameStates.WinCivilianReady;
                                }
                            }
                        }
                    }
                    
                    if (activeUnits.Count == 0)
                    {
                        gameState = GameStates.GameOverReady;
                    }


                    break;

                case GameStates.GameOverReady:

                    backgroundMusic.Stop(AudioStopOptions.Immediate);

                    gameState = GameStates.GameOver;

                    foreach (Tile tile in map.Tiles)
                    {
                        tile.IsVisible = true;
                    }

                    break;


                case GameStates.GameOver:


                    foreach (Zombie zombie in activeZombies)
                    {
                        zombie.Update(gameTime, FindEnemyUnits(zombie), FindFriendlyUnits(zombie));
                    }

                    if (gamePad.Buttons.A == ButtonState.Pressed)
                    {
                        gameState = GameStates.TitleScreenReady;
                        InitializeNewGame();
                    }
                    break;

                case GameStates.WinCivilianReady:

                    backgroundMusic.Stop(AudioStopOptions.Immediate);
                    endingMusic.Play();
                    gameState = GameStates.WinCivilian;
                    break;

                case GameStates.WinCivilian:

                    if (gamePad.Buttons.A == ButtonState.Pressed)
                    {
                        gameState = GameStates.TitleScreenReady;
                        //InitializeNewGame();
                    }
                    break;

                case GameStates.WinMilitaryReady:
                    
                    backgroundMusic.Stop(AudioStopOptions.Immediate);
                    endingMusic.Play();
                    gameState = GameStates.WinMilitary;
                    break;

                case GameStates.WinMilitary:
                    if (gamePad.Buttons.A == ButtonState.Pressed)
                    {
                        gameState = GameStates.TitleScreenReady;
                        //InitializeNewGame();
                    }
                    break;

                case GameStates.WinScienceReady:

                    backgroundMusic.Stop(AudioStopOptions.Immediate);
                    endingMusic.Play();
                    gameState = GameStates.WinScience;
                    break;

                case GameStates.WinScience:
                    if (gamePad.Buttons.A == ButtonState.Pressed)
                    {
                        gameState = GameStates.TitleScreenReady;
                        //InitializeNewGame();
                    }
                    break;

                default:
                    break;
            }

            base.Update(gameTime);
        }


        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            graphics.GraphicsDevice.Clear(Color.Black);

            switch (gameState)
            {

                case GameStates.XboxScreen:

                    spritebatch.Begin();

                    spritebatch.Draw(xBoxLogoScreen, new Rectangle(0, 0, graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Height), Color.White);

                    spritebatch.End();

                    break;

                case GameStates.StudioScreen:

                    spritebatch.Begin();

                    spritebatch.Draw(studioScreen, new Rectangle(0, 0, graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Height), Color.White);

                    spritebatch.End();

                    break;

                case GameStates.TitleScreen:

                    spritebatch.Begin();

                    spritebatch.Draw(titleScreen, new Rectangle(0, 0, graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Height), Color.White);

                    spritebatch.DrawString(spriteFont, "Press Start", new Vector2((graphics.GraphicsDevice.Viewport.Width / 2) -spriteFont.MeasureString("Press Start").X/2, graphics.GraphicsDevice.Viewport.Height - 30), Color.White);


                    spritebatch.End();
                    break;

                case GameStates.ControlsScreen:

                    spritebatch.Begin();

                    spritebatch.Draw(controlsScreen, new Rectangle(0, 0, graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Height), Color.White);

                    spritebatch.End();
                    break;

                case GameStates.StoryScreen:

                    spritebatch.Begin();

                    spritebatch.DrawString(spriteFont, "You have just awakened to a nightmare...\nA deserted city crawling with diseased victims trying to kill you.\n" +
                        "Once humans, now zombies...\nThey have one goal, to eat human flesh\n" +
                        "You hear a strange sound..maybe there are other survivors...", new Vector2(100, 50), Color.White);


                    spritebatch.DrawString(spriteFont, "Loading...", new Vector2((graphics.GraphicsDevice.Viewport.Width / 2), (graphics.GraphicsDevice.Viewport.Height/2)), Color.White);
                    

                    spritebatch.End();

                    break;

                case GameStates.InGame:

                    spritebatch.Begin();

                    map.DrawTown(spritebatch);

                    foreach (Building building in buildings)
                    {
                        building.Draw(spritebatch, map.FocusX, map.FocusY, gameTime);
                    }

                    foreach (RoadBlock roadBlock in activeRoadBlocks)
                    {
                        roadBlock.Draw(spritebatch, map.FocusX, map.FocusY);
                    }
                    
                    foreach (Trap trap in activeTraps)
                    {
                        trap.Draw(spritebatch, map.FocusX, map.FocusY);
                    }

                    foreach (Unit unit in activeZombies)
                    {
                        unit.Draw(spritebatch, map.FocusX, map.FocusY);
                    }

                    foreach (Unit unit in activeUnits)
                    {
                        unit.Draw(spritebatch, map.FocusX, map.FocusY);
                    }

                    

                    foreach (Unit unit in deadUnits)
                    {
                        unit.Draw(spritebatch, map.FocusX, map.FocusY);
                    }








                    map.DrawTiles(spritebatch);

                    gameSelection.Draw(spritebatch, map.FocusX, map.FocusY);
                    hud.Draw(spritebatch);

                    gameCursor.Draw(spritebatch);

                   spritebatch.End();
                    break;

                case GameStates.GameOver:

                    spritebatch.Begin();

                    map.DrawTown(spritebatch);

                    foreach (Building building in buildings)
                    {
                        building.Draw(spritebatch, map.FocusX, map.FocusY, gameTime);
                    }

                    foreach (RoadBlock roadBlock in activeRoadBlocks)
                    {
                        roadBlock.Draw(spritebatch, map.FocusX, map.FocusY);
                    }

                    foreach (Trap trap in activeTraps)
                    {
                        trap.Draw(spritebatch, map.FocusX, map.FocusY);
                    }

                    foreach (Unit unit in activeZombies)
                    {
                        unit.Draw(spritebatch, map.FocusX, map.FocusY);
                    }

                    foreach (Unit unit in deadUnits)
                    {
                        unit.Draw(spritebatch, map.FocusX, map.FocusY);
                    }


                    map.DrawTiles(spritebatch);

                    //gameSelection.Draw(spritebatch, map.FocusX, map.FocusY);
                    hud.Draw(spritebatch);

                    //gameCursor.Draw(spritebatch);

                    spritebatch.Draw(gameOver, new Rectangle(0, 0, graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Height), Color.White);


                    spritebatch.End();

                    break;
                case GameStates.WinCivilian:
                    spritebatch.Begin();
                    spritebatch.Draw(civilianWin, new Rectangle(0, 0, graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Height), Color.White);
                    spritebatch.End();
                    break;

                case GameStates.WinMilitary:
                    spritebatch.Begin();
                    spritebatch.Draw(militaryWin, new Rectangle(0, 0, graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Height), Color.White);
                    spritebatch.End();
                    break;

                case GameStates.WinScience:
                    spritebatch.Begin();
                    spritebatch.Draw(scienceWin, new Rectangle(0, 0, graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Height), Color.White);
                    spritebatch.End();
                    break;

            }

            base.Draw(gameTime);
        }


        public List<Human> GetSelection()
        {
            List<Human> selected = new List<Human>();

            if (gameSelection.StartX == gameSelection.EndX
                && gameSelection.StartY == gameSelection.EndY)
            {
                foreach (Human human in activeUnits)
                {
                    if (gameSelection.StartX >= human.Position.X
                        && gameSelection.StartX <= human.Position.X + Tile.TILEWIDTH
                        && gameSelection.StartY >= human.Position.Y
                        && gameSelection.StartY <= human.Position.Y + Tile.TILEHEIGHT
                        && human.InBuilding == false)
                    {
                        selected.Add(human);
                        return selected;
                    }
                }

                foreach (Building building in buildings)
                {
                    building.IsSelected = false;

                    if (gameSelection.StartX >= building.Position.X
                        && gameSelection.StartX <= building.Position.X + building.Width
                        && gameSelection.StartY >= building.Position.Y
                        && gameSelection.StartY <= building.Position.Y + building.Height
                    )
                    {
                        hud.SelectBuilding(building);

                        building.IsSelected = true;

                        return selectedItems;
                    }
                }

                foreach (RoadBlock roadBlock in activeRoadBlocks)
                {



                    if (gameSelection.StartX >= roadBlock.Position.X
                        && gameSelection.StartX <= roadBlock.Position.X + roadBlock.Width
                        && gameSelection.StartY >= roadBlock.Position.Y
                        && gameSelection.StartY <= roadBlock.Position.Y + roadBlock.Height
                    )
                    {
                        hud.SelectRoadBlock(roadBlock);



                        return selectedItems;
                    }
                }


            }
            else
            {

                foreach (Human unit in activeUnits)
                {
                    if (gameSelection.StartX > gameSelection.EndX)
                    {
                        if (gameSelection.StartY >= gameSelection.EndY)
                        {
                            if (unit.Position.X + 16 > gameSelection.EndX
                                && unit.Position.X + 16 < gameSelection.StartX
                                && unit.Position.Y + 16 > gameSelection.EndY
                                && unit.Position.Y + 16 < gameSelection.StartY
                                && unit.InBuilding == false
                                )
                            {
                                selected.Add(unit);
                            }
                        }
                        else if (gameSelection.StartY < gameSelection.EndY)
                        {
                            if (unit.Position.X + 16 > gameSelection.EndX
                                && unit.Position.X + 16 < gameSelection.StartX
                                && unit.Position.Y + 16 < gameSelection.EndY
                                && unit.Position.Y + 16 > gameSelection.StartY
                                && unit.InBuilding == false
                                )
                            {
                                selected.Add(unit);
                            }
                        }
                    }
                    else if (gameSelection.StartX < gameSelection.EndX)
                    {
                        if (gameSelection.StartY >= gameSelection.EndY)
                        {
                            if (unit.Position.X + 16 < gameSelection.EndX
                               && unit.Position.X + 16 > gameSelection.StartX
                               && unit.Position.Y + 16 > gameSelection.EndY
                               && unit.Position.Y + 16 < gameSelection.StartY
                               && unit.InBuilding == false
                               )
                            {
                                selected.Add(unit);
                            }
                        }
                        else if (gameSelection.StartY < gameSelection.EndY)
                        {
                            if (unit.Position.X + 16 < gameSelection.EndX
                                && unit.Position.X + 16 > gameSelection.StartX
                                && unit.Position.Y + 16 < gameSelection.EndY
                                && unit.Position.Y + 16 > gameSelection.StartY
                                && unit.InBuilding == false
                                )
                            {
                                selected.Add(unit);
                            }
                        }


                    }

                }
            }

            if (selected.Count > 12)
            {
                selected.RemoveRange(12, selected.Count - 12);
            }

            return selected;
        }

        public Unit GetTargetUnit(Vector2 target)
        {
            Unit targetUnit = null;

            foreach (Unit unit in activeUnits)
            {
                if (target.X - 7.0f < unit.Position.X
                    && target.X + 7.0f > unit.Position.X
                    && target.Y - 7.0f < unit.Position.Y
                    && target.Y + 7.0f > unit.Position.Y)
                {
                    targetUnit = unit;
                }
            }

            return targetUnit;
        }

        public List<Unit> FindEnemyUnits(Unit curUnit)
        {
            List<Unit> enemies = new List<Unit>();

            if (curUnit.IsZombie == true)
            {

                foreach (Unit unit in activeUnits)
                {
                    if (unit.InBuilding == false)
                    {
                        Vector2 distance = unit.Position - curUnit.Position;

                        if (distance.Length() <= curUnit.AttackRadius && unit.IsDead == false)
                        {
                            enemies.Add(unit);
                        }
                    }
                }
            }
            else
            {
                foreach (Unit unit in activeZombies)
                {
                    if (unit.InBuilding == false)
                    {
                        Vector2 distance = unit.Position - curUnit.Position;

                        if (distance.Length() <= curUnit.AttackRadius && unit.IsDead == false)
                        {
                            enemies.Add(unit);
                        }
                    }
                }
            }

            return enemies;
        }

        public static List<Unit> FindEnemyUnits(Unit curUnit, float actionRadius)
        {
            List<Unit> enemies = new List<Unit>();

            if (curUnit.IsZombie == true)
            {

                foreach (Unit unit in activeUnits)
                {
                    if (unit.InBuilding == false)
                    {
                        Vector2 distance = unit.Position - curUnit.Position;

                        if (distance.Length() <= actionRadius && unit.IsDead == false)
                        {
                            enemies.Add(unit);
                        }
                    }
                }
            }
            else
            {
                foreach (Unit unit in activeZombies)
                {
                    if (unit.InBuilding == false)
                    {
                        Vector2 distance = unit.Position - curUnit.Position;

                        if (distance.Length() <= actionRadius && unit.IsDead == false)
                        {
                            enemies.Add(unit);
                        }
                    }
                }
            }

            return enemies;
        }


        public List<Unit> FindFriendlyUnits(Unit curUnit)
        {
            List<Unit> friends = new List<Unit>();

            if (curUnit.IsZombie == false)
            {

                foreach (Unit unit in activeUnits)
                {

                    if (unit.InBuilding == false)
                    {
                        Vector2 distance = unit.Position - curUnit.Position;

                        if (distance.Length() <= (curUnit.SightRadius * 32) && unit.IsDead == false)
                        {
                            friends.Add(unit);
                        }
                    }
                }
            }
            else
            {
                foreach (Unit unit in activeZombies)
                {
                    if (unit.InBuilding == false)
                    {
                        Vector2 distance = unit.Position - curUnit.Position;

                        if (distance.Length() <= (curUnit.SightRadius * 32) && unit.IsDead == false)
                        {
                            friends.Add(unit);
                        }
                    }
                }
            }

            return friends;
        }

        public void RemoveDeadUnits(List<Unit> removeList)
        {
            foreach (Unit unit in removeList)
            {
                deadUnits.Add(unit);
                if (unit.IsZombie == false)
                {
                    activeUnits.Remove((Human)unit);
                    selectedItems.Remove((Human)unit);
                }
                else
                {
                    activeZombies.Remove((Zombie)unit);
                }
            }
        }

        public void RemoveDeadRoadBlocks(List<RoadBlock> removeList)
        {
            foreach (RoadBlock rb in removeList)
            {
                deadRoadBlocks.Add(rb);
                activeRoadBlocks.Remove(rb);
            }
        }

        public void RemoveDeadTraps(List<Trap> removeList)
        {
            foreach (Trap trap in removeList)
            {
                activeTraps.Remove(trap);
            }
        }

        public static RoadBlock IsCollidingWithRoadBlock(Zombie zombie)
        {
            foreach (RoadBlock rb in activeRoadBlocks)
            {
                if (rb.Position.X < zombie.Position.X + 16 &&
                    rb.Position.X + rb.Width > zombie.Position.X + 16 &&
                    rb.Position.Y < zombie.Position.Y + 16 &&
                    rb.Position.Y + rb.Height > zombie.Position.Y + 16 &&
                    rb.IsConstructed == true)
                {
                    return rb;
                }
            }

            return null;
        }

        public static Trap IsCollidingWithTrap(Zombie zombie)
        {
            foreach (Trap rb in activeTraps)
            {
                if (rb.Position.X < zombie.Position.X + 16 &&
                    rb.Position.X + rb.Width > zombie.Position.X + 16 &&
                    rb.Position.Y < zombie.Position.Y + 16 &&
                    rb.Position.Y + rb.Height > zombie.Position.Y + 16 &&
                    rb.IsConstructed == true)
                {
                    return rb;
                }
            }

            return null;
        }



        public static Vector2 GetNearestHumanEntity(Unit unit)
        {
            Human nearest;

            //check to see if there are any active units
            if (activeUnits.Count != 0)
            {
                nearest = activeUnits[0];
                float mindist = Vector2.Distance(unit.Position, nearest.Position);
                float xdist;
                foreach (Human h in activeUnits)
                {
                    xdist = Vector2.Distance(unit.Position, h.Position);
                    if (xdist < mindist)
                    {
                        mindist = xdist;
                        nearest = h;
                    }
                }
                return nearest.Position;

            }
            else
            {
                return GetBase().Entrance;
            }


        }

        public static bool RoadBlockIsAcceptable(RoadBlock roadBlock)
        {
            Vector2 rbPosition = roadBlock.Position;

            rbPosition = new Vector2((int)((rbPosition.X) / 32), (int)((rbPosition.Y) / 32));

            if (rbPosition.X < 0 || rbPosition.X > map.Tiles.GetLength(0) - 1)
            {
                return false;
            }

            if (rbPosition.Y < 0 || rbPosition.Y > map.Tiles.GetLength(1) - 1)
            {
                return false;
            }

            if (roadBlock.IsVertical)
            {
                if (map.Tiles[(int)rbPosition.X, (int)rbPosition.Y].IsWalkable == false)
                {
                    return false;
                }
                if (map.Tiles[(int)rbPosition.X, (int)rbPosition.Y + 1].IsWalkable == false)
                {
                    return false;
                }
            }
            else
            {
                if (map.Tiles[(int)rbPosition.X, (int)rbPosition.Y].IsWalkable == false)
                {
                    return false;
                }
                if (map.Tiles[(int)rbPosition.X + 1, (int)rbPosition.Y].IsWalkable == false)
                {
                    return false;
                }
            }

            return true;
        }

        public static bool RoadTrapIsAcceptable(Trap trap)
        {
            Vector2 rbPosition = trap.Position;

            rbPosition = new Vector2((int)((rbPosition.X) / 32), (int)((rbPosition.Y) / 32));

            if (rbPosition.X < 0 || rbPosition.X > map.Tiles.GetLength(0) - 1)
            {
                return false;
            }

            if (rbPosition.Y < 0 || rbPosition.Y > map.Tiles.GetLength(1) - 1)
            {
                return false;
            }


            if (map.Tiles[(int)rbPosition.X, (int)rbPosition.Y].IsWalkable == false)
            {
                return false;
            }


            return true;
        }

        public static Base GetBase()
        {
            foreach (Building building in buildings)
            {
                if (building.BuildingType == BuildingType.Base)
                {
                    return (Base)building;
                }
            }

            return null;
        }

        public void InitializeBuildings()
        {

            List<Texture2D> twos = new List<Texture2D>();
            List<Texture2D> threes = new List<Texture2D>();
            List<Texture2D> fours = new List<Texture2D>();
            List<Texture2D> fives = new List<Texture2D>();

            twos.Add(content.Load<Texture2D>("Content\\Txtr\\Bldg\\TBldg\\building_house_a"));
            twos.Add(content.Load<Texture2D>("Content\\Txtr\\Bldg\\TBldg\\building_house_b"));
            twos.Add(content.Load<Texture2D>("Content\\Txtr\\Bldg\\TBldg\\building_house_c"));
            twos.Add(content.Load<Texture2D>("Content\\Txtr\\Bldg\\TBldg\\building_house_d"));
            twos.Add(content.Load<Texture2D>("Content\\Txtr\\Bldg\\TBldg\\building_house_e"));
            twos.Add(content.Load<Texture2D>("Content\\Txtr\\Bldg\\TBldg\\building_house_f"));
            twos.Add(content.Load<Texture2D>("Content\\Txtr\\Bldg\\TBldg\\building_house_g"));
            twos.Add(content.Load<Texture2D>("Content\\Txtr\\Bldg\\TBldg\\building_house_h"));
            twos.Add(content.Load<Texture2D>("Content\\Txtr\\Bldg\\TBldg\\building_house_i"));
            twos.Add(content.Load<Texture2D>("Content\\Txtr\\Bldg\\TBldg\\building_house_j"));
            twos.Add(content.Load<Texture2D>("Content\\Txtr\\Bldg\\TBldg\\building_house_k"));
            twos.Add(content.Load<Texture2D>("Content\\Txtr\\Bldg\\TBldg\\building_house_l"));
            twos.Add(content.Load<Texture2D>("Content\\Txtr\\Bldg\\TBldg\\building_house_m"));
            twos.Add(content.Load<Texture2D>("Content\\Txtr\\Bldg\\TBldg\\building_house_n"));
            twos.Add(content.Load<Texture2D>("Content\\Txtr\\Bldg\\TBldg\\building_house_o"));
            twos.Add(content.Load<Texture2D>("Content\\Txtr\\Bldg\\TBldg\\building_house_p"));
            twos.Add(content.Load<Texture2D>("Content\\Txtr\\Bldg\\TBldg\\building_house_q"));
            twos.Add(content.Load<Texture2D>("Content\\Txtr\\Bldg\\TBldg\\building_house_r"));
            twos.Add(content.Load<Texture2D>("Content\\Txtr\\Bldg\\TBldg\\building_house_s"));
            twos.Add(content.Load<Texture2D>("Content\\Txtr\\Bldg\\TBldg\\building_house_t"));




            threes.Add(content.Load<Texture2D>("Content\\Txtr\\Bldg\\TBldg\\building_96x96_a"));
            threes.Add(content.Load<Texture2D>("Content\\Txtr\\Bldg\\TBldg\\building_96x96_b"));
            threes.Add(content.Load<Texture2D>("Content\\Txtr\\Bldg\\TBldg\\building_96x96_c"));
            threes.Add(content.Load<Texture2D>("Content\\Txtr\\Bldg\\TBldg\\building_96x96_d"));
            threes.Add(content.Load<Texture2D>("Content\\Txtr\\Bldg\\TBldg\\building_96x96_e"));
            threes.Add(content.Load<Texture2D>("Content\\Txtr\\Bldg\\TBldg\\building_96x96_f"));

            fours.Add(content.Load<Texture2D>("Content\\Txtr\\Bldg\\TBldg\\building_128x128_a"));
            fours.Add(content.Load<Texture2D>("Content\\Txtr\\Bldg\\TBldg\\building_128x128_b"));
            fours.Add(content.Load<Texture2D>("Content\\Txtr\\Bldg\\TBldg\\building_128x128_c"));
            fours.Add(content.Load<Texture2D>("Content\\Txtr\\Bldg\\TBldg\\building_128x128_d"));

            fives.Add(content.Load<Texture2D>("Content\\Txtr\\Bldg\\TBldg\\building_160x160_a"));
            fives.Add(content.Load<Texture2D>("Content\\Txtr\\Bldg\\TBldg\\building_160x160_b"));

            Random rand = new Random();


            StreamReader file = new StreamReader("Content\\Sys\\2by2.txt");

            string nextString = file.ReadLine();

            while (nextString != null)
            {
                string[] strings = nextString.Split(',');

                int random = rand.Next(100);
                int zombieCount = 0;

                if(random > 60)
                {
                    zombieCount = rand.Next(5);
                }

                Building b = new Building(content, twos[rand.Next(twos.Count)], "", new Vector2(Int32.Parse(strings[0]) * 32, Int32.Parse(strings[1]) * 32), new Vector2(Int32.Parse(strings[2]) * 32, Int32.Parse(strings[3]) * 32), zombieCount);
                buildings.Add(b);

                if (Vector2.Distance(b.Position, SolanumSolstice.GetBase().Position) < 500)
                {
                    houseList.Add(b);
                }

                nextString = file.ReadLine();
            }

            StreamReader file2 = new StreamReader("Content\\Sys\\3by3.txt");

            string nextString2 = file2.ReadLine();

            while (nextString2 != null)
            {
                string[] strings = nextString2.Split(',');

                int random = rand.Next(100);
                int zombieCount = 0;

                if (random > 60)
                {
                    zombieCount = rand.Next(7);
                }

                buildings.Add(new Building(content, threes[rand.Next(threes.Count)], "", new Vector2(Int32.Parse(strings[0]) * 32, Int32.Parse(strings[1]) * 32), new Vector2(Int32.Parse(strings[2]) * 32, Int32.Parse(strings[3]) * 32), zombieCount));

                nextString2 = file2.ReadLine();
            }

            StreamReader file3 = new StreamReader("Content\\Sys\\4by4.txt");

            string nextString3 = file3.ReadLine();

            while (nextString3 != null)
            {
                string[] strings = nextString3.Split(',');

                int random = rand.Next(100);
                int zombieCount = 0;

                if (random > 70)
                {
                    zombieCount = rand.Next(9);
                }

                buildings.Add(new Building(content, fours[rand.Next(fours.Count)], "", new Vector2(Int32.Parse(strings[0]) * 32, Int32.Parse(strings[1]) * 32), new Vector2(Int32.Parse(strings[2]) * 32, Int32.Parse(strings[3]) * 32), zombieCount));

                nextString3 = file3.ReadLine();
            }

            StreamReader file4 = new StreamReader("Content\\Sys\\5by5.txt");

            string nextString4 = file4.ReadLine();

            while (nextString4 != null)
            {
                string[] strings = nextString4.Split(',');

                int random = rand.Next(100);
                int zombieCount = 0;

                if (random > 70)
                {
                    zombieCount = rand.Next(13);
                }

                buildings.Add(new Building(content, fives[rand.Next(fives.Count)], "", new Vector2(Int32.Parse(strings[0]) * 32, Int32.Parse(strings[1]) * 32), new Vector2(Int32.Parse(strings[2]) * 32, Int32.Parse(strings[3]) * 32), zombieCount));

                nextString4 = file4.ReadLine();
            }

        }
    }
}


